//消防报警任务的相关js
/**
 * 初始化任务列表
 */
var tasks;
function initFireTask() {
    getTaskList({
        data: {taskt: 3},
        init: function(taskData){
            //返回实时音乐列表行
            return fireHtml(taskData);
        }
    });
}

/**
 * 显示报警
 * @param data
 */
function fireHtml(data){
    var html = "";
    html += "<tr id='tr_task_" + data.id + "' taskid='" + data.id + "' taskidstr='" + data.taskid + "' onclick='fireTaskTr(" + data.id + ");'>";
    html += "<td>" + data.taskname + "</td>";
    html += "<td id='task_status_" + data.taskid + "'>";
    if(data.status == 0){
        html += "任务空闲";
    } else if(data.status == 1){
        html += "执行中";
    } else if(data.status == 2){
        html += "任务空闲";
    } else if(data.status == 3){
        html += "暂停";
    }
    html += "<td>" + data.port + "</td>";
    html += "<td>" + data.area + "</td>";
    html += "<td>" +(data.isLevel == 2 ? '开启' : '关闭') + "</td>";
    html += "<td>" + data.mp3 + "</td>";
    html += "<td>" + data.users + "</td>";
    html += "</tr>";
    return html;
}

/**
 * 点击行
 */
function fireTaskTr(taskid) {
    trtask({
        taskid: taskid,
        callback: function(data){
            var mp3s = data.mp3;
            if (mp3s) {
                var html = "";
                for (var i = 0; i < mp3s.length; i++) {
                    html += "<tr><td>" + mp3s[i] + "</td></tr>";
                }
                $("#mp3_list").html(html);
            }

            var clients = data.clients;
            if (clients) {
                var html = "";
                for (var i = 0; i < clients.length; i++) {
                    html += "<tr><td>" + clients[i].terminalname + "</td></tr>";
                }
                $("#clients_list").html(html);
            }
        }
    });
}

/**
 * 获得所有在线设备
 */
function initOnLineClient(data){
    var userids = new Array();
    var html = "";
    A:for(var i = 0; i < data.length; i++){
        if(data[i].isClient == 1){

            for(var j = 0; j < userids.length; j++){
                if(userids[j] == data[i].userid){
                    continue A;
                }
            }

            userids.push(data[i].userid);


            html += "<option value='" + data[i].userid + "'>" + data[i].gname + "</option>";
        }
    }
    $("#externalFireTerminal").html(html);
}

//开放或者禁用持续时间
function open_fire(flag) {
    if (!flag) {
        $("#externalFireTerminal").attr("disabled", "disabled");
    } else {
        $("#externalFireTerminal").removeAttr("disabled");
        alert("开启外部采集，上传的曲目将会无效！");
    }

    renderForm();
}

//---------------------提交任务相关-------------------------
/**
 * 提交任务
 */
function submit_task() {
    //获得任务名称
    taskname = $("#taskname").val();

    //获得报警区域
    area = $("#area").val();

    //获得报警触发设备
    var fireTerminal = $("#fireTerminal").val();
    //获得端口
    port = $("#port").val();


    //获得邻层报警
    var isLevel = $("#isLevel").val();
    //获得指定外部音源
    isExternalMusic = $("#isExternalMusic").is(':checked') ? 1 : 0;
    //获得外部报警设备
    var externalFireTerminal = $("#externalFireTerminal").val();

    //获得被选择的终端
    var ztreeObj = getZtree();
    var znodes = ztreeObj.getCheckedNodes(true);
    terminal = [];
    A:for (var i = 0; i < znodes.length; i++) {
        if (znodes[i].isClient == 1) {
            for(var j = 0; j < terminal.length; j++){
                if(terminal[j] == znodes[i].userid){
                    continue A;
                }
            }
            //如果是终端
            terminal.push(znodes[i].userid);
        }
    }

    //表单验证
    if(!check_Form(3)){
        return;
    }

    //ajax提交任务
    myajax({
        type: "POST",
        url: '/task/insert',
        data: {
            "mp3": JSON.stringify(files),
            "users": JSON.stringify(terminal),
            "taskname": taskname,
            "area": area,
            "fireTerminal": fireTerminal,
            "taskt": 3,
            "port": port,
            "isLevel": isLevel,
            "isExternalMusic": isExternalMusic,
            "externalFireTerminal": externalFireTerminal
        },
        success: function (data) {
            //发送websocket
            console.log(data);

            var html = fireHtml(data);
            $("#task_list").append(html);

            //关闭弹出框
            layer.closeAll();
        }
    });
}

/**
 * 提交修改任务
 * @param tid
 */
function update_task(tid){
    //获得任务名称
    var taskname = $("#taskname").val();
    //获得报警区域
    var area = $("#area").val();
    //获得报警触发设备
    var fireTerminal = $("#fireTerminal").val();
    //获得端口
    var port = $("#port").val();
    //获得邻层报警
    var isLevel = $("#isLevel").val();
    //获得指定外部音源
    var isExternalMusic = $("#isExternalMusic").is(':checked') ? 1 : 0;
    //获得外部报警设备
    var externalFireTerminal = $("#externalFireTerminal").val();


    //获得被选择的终端
    var ztreeObj = getZtree();
    var znodes = ztreeObj.getCheckedNodes(true);
    var terminal = [];
    A:for (var i = 0; i < znodes.length; i++) {
        if (znodes[i].isClient == 1) {
            for(var j = 0; j < terminal.length; j++){
                if(terminal[j] == znodes[i].userid){
                    continue A;
                }
            }
            //如果是终端
            terminal.push(znodes[i].userid);
        }
    }

    //ajax提交任务
    myajax({
        type: "POST",
        url: '/task/update',
        data: {
            "mp3": JSON.stringify(files),
            "users": JSON.stringify(terminal),
            "taskname": taskname,
            "area": area,
            "fireTerminal": fireTerminal,
            "taskt": 3,
            "port": port,
            "isLevel": isLevel,
            "isExternalMusic": isExternalMusic,
            "externalFireTerminal": externalFireTerminal,
            "taskid": tid
        },
        success: function (data) {
            //发送websocket
            console.log(data);

            //移除原来的节点
            $("#tr_task_" + data.id).remove();

            var html = fireHtml(data);
            $("#task_list").append(html);

            //关闭弹出框
            layer.closeAll();
        }
    });
}